package me.seu.demo.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class OpUtil {

    /**
     * 计算两数差分取绝对值
     */
    private static Integer getDiff(Integer b1, Integer b2) {
        return Math.abs(b2 - b1);
    }

    /**
     * 计算数组差分取绝对值
     */
    private static Integer[] getDiffArray(int[] array) {
        Integer[] result = new Integer[array.length - 1];
        for (int i = 0; i < array.length - 1; i++) {
            result[i] = getDiff(array[i + 1], array[i]);
        }
        return result;
    }

    /**
     * 获取数组中最大最小值差值
     */
    private static Integer getMaxMinDiff(int[] array) {
        int max = array[0];
        int min = array[0];
        for (Integer value : array) {
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
        return max - min;
    }

    /**
     * 获取数组差分占比列表
     */
    public static BigDecimal[] getDiffPartList(int[] array) {
        BigDecimal maxDiff = new BigDecimal(getMaxMinDiff(array));
        Integer[] diffArray = getDiffArray(array);

        BigDecimal[] result = new BigDecimal[diffArray.length];
        for (int i = 0; i < diffArray.length; i++) {
            try {
                result[i] = new BigDecimal(diffArray[i]).divide(maxDiff, 2, RoundingMode.HALF_UP);
            } catch (Exception e) {
                log.error("error index={}", i, e);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String s = "154,110,117,128,113,136,131,136,137,127,121,137,117,136,119,122,120,152,116,118,140,124,122,143," +
                "123,120,147,119,129,122,113,135,130,127,130,131,150,112,118,150,110,125,142,121,113,145,129,132,136," +
                "110,126,118,120,143,118,122,143,132,126,133,129,125,141,123,114,112,142,108,106,113,106,140,112,113," +
                "135,120,116,135,130,124,118,114,126,139,137,119,118,123,149,130,123,110,141,129,122,110,123,121,113," +
                "97,116,131,125,126,107,122,124,111,120,131,104,135,118,122,144,119,105,134,128,130,132,130,106,112," +
                "117,111,119,116,117,129,124,133,118,128,110,112,123,118,104,123,141,129,141,125,117,126,143,136,148," +
                "124,132,122,131,144,107,113,128,147,117,124,123,123,142,125,127,126,108,125,110,131,129,124,106,147," +
                "134,109,106,122,152,115,137,124,128,130,125,151,123,134,147,138,111,124,119,145,116,127,134,112,134," +
                "132,115,125,116,129,144,121,140,143,134,123,137,122,136,112,109,131,132,141,130,130,128,118,157,150," +
                "135,136,122,144,120,124,138,115,146,141,141,123,118,140,130,132,140,115,107,112,140,127,141,130,121," +
                "143,122,120,123,114,127,116,140,147,141,108,177,120,130,141,149,132,135,123,124,152,114,110,1358," +
                "2004,1866,1053,1124,1294,1223,1394,1472,1410,1139,1031,1559,4,1203,1346,1153,1531,1435,1995,1531," +
                "1919,1880,1320,1073,953,2235,1282,989,1870,1299,1106,1263,1274,1661,1054,1186,1105,2039,3713,1139," +
                "2180,1332,1466,1166,1407,1381,1890,2352,2259,1531,1733,1360,1276,1913,1378,1151,2134,2004,1676,982," +
                "1373,1581,1100,972,1083,1332,1443,1236,1364,1425,1177,1539,1130,1269,1234,1360,1312,898,1359,1460," +
                "1052,1277,1305,1460,1258,1238,978,1013,1288,1429,1446,1287,2026,1298,1544,1267,1298,2314,1230,1421," +
                "1141,1269,1686,1704,1270,1684,1435,1210,1637,1363,1192,1547,1191,1381,1372,1460,1536,2185,1399,1961," +
                "2485,1034,1157,1398,1358,1841,1699,1391,1412,986,1280,1334,5,1030,1549,1329,1174,1188,1114,1298," +
                "1148,1243,1063,1087,1119,1037,1167,1244,2249,1161,1163,1756,1519,1364,1360,4,1111,2395,1440,1131," +
                "1425,1390,1548,1532,1708,1631,1436,1685,1490,1400,1396,1475,1910,1258,1119,1402,1771,1024,1362,1109," +
                "1172,2081,2064,1393,1742,1555,1582,1824,2609,1421,1289,1915,1238,1358,1492,1341,1597,1243,1280,1030," +
                "1365,1178,1606,1301,1562,1715,1290,1403,1149,1613,1595,1610,1190,635,1355,1665,2232,1535,1328,786," +
                "2509,921,1029,1542,1435,1834,1063,1108,1355,1104,845,3445,932,764,2311,2132,2040,2203,2117,698,1179," +
                "1814,2126,1388,1420,1753,2567,842,683,1015,828,1023,775,1059,1893,1880,989,1291,1210,1692,1690,2240," +
                "730,1418,1123,665,1815,1680,2129,1409,1529,1748,1293,4744,2093,4465,1470,1543,2465,1327,961,1296," +
                "1131,136,121,136,150,111,140,149,149,156,110,133,134,145,164,148,166,112,114,112,138,124,139,120," +
                "136,115,121,115,114,109,134,136,122,154,114,113,123,126,127,112,119,156,126,122,138,147,149,132,182," +
                "95,125,134,121,111,127,133,130,123,119,149,149,184,171,164,141,129,128,130,134,131,124,128,121,126," +
                "144,128,150,147,130,151,162,152,146,130,117,114,154,137,124,122,117,130,117,139,124,139,145,129,150," +
                "102,142,125,133,137,116,142,128,128,132,145,121,126,117,1519,1480,1703,3933,1490,1265,1151,1271," +
                "1120,1955,1350,1174,2050,124,124,126,115,141,136,131,136,137,129,120,117,144,119,123,124,142,117," +
                "123,133,128,139,159,118,121,119,129,124,124,116,135,143,117,129,126,111,146,132,134,124,118,108,141," +
                "130,127,118,131,124,127,125,142,133,127,130,121,138,120,132,134,127,110,111,135,143,120,120,119,129," +
                "118,111,141,111,124,123,137,100,119,153,146,122,126,146,122,128,152,109,117,107,131,120,118,120,103," +
                "127,122,132,134,130,123,132,129,112,151,124,144,120,141,153,132,113,124,133,148,153,127,117,106,133," +
                "122,110,147,122,145,142,127,126,123,118,129,136,114,128,122,126,140,112,131,115,161,109,120,112,124," +
                "113,133,123,129,107,121,113,134,128,144,126,136,126,137,124,134,120,127,138,122,120,114,122,115,135," +
                "129,114,119,125,121,114,116,136,138,131,118,113,113,107,129,132,134,111,128,147,130,113,128,132,108," +
                "127,139,124,132,123,132,114,115,148,130,113,122,114,119,139,125,134,121,116,134,119,130,129,130,125," +
                "118,118,121,106,148,120,139,146,126,106,137,121,114,108,116,115,112,99,116,106,129,123,125,112,97," +
                "139,132,126,122,124";
        String[] split = s.split(",");
        int[] array = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();

        // 波动阈值
        BigDecimal stop = new BigDecimal("0.1");
        // 开工采样时长
        int opNum = 18;

        List<Integer> pointList = new ArrayList<>();
        int firstPoint = 0;
        BigDecimal[] list = getDiffPartList(array);

        for (int i = 0; i < list.length; i++) {
            BigDecimal decimal = list[i];
            if (decimal.compareTo(stop) > 0) {
                if (i - firstPoint > opNum) {
                    pointList.add(i);
                    firstPoint = i;
                }
            }
        }

        System.out.println(pointList);
    }

}
